package com.weixin.controller;


import com.github.pagehelper.PageHelper;
import com.weixin.entity.Wages;
import com.weixin.service.IWagesService;
import com.weixin.uitl.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gpf
 * @since 2018-12-24
 */
@RestController
@RequestMapping("/wages")
public class WagesController {

    @Autowired
    private IWagesService wagesService;

    @GetMapping("/list")
    public R listAll(@RequestParam(value = "pageNumber",defaultValue = "1")Integer pageNumber,@RequestParam(value = "pageSize",defaultValue = "6")Integer pageSize){
        /**
         * 查询所有
         */
        PageHelper.startPage(pageNumber,pageSize);
        List<Wages> employeeList = wagesService.list();
        /**
         * 查询总条目
         */
        int total = wagesService.count();
        return R.ok().put("total",total).put("rows",employeeList);
    }

    /**
     * 添加
     * @param wages
     * @return
     */
    @GetMapping("/add")
    public R add(Wages wages){
        boolean b = wagesService.save(wages);
        if (b){
            return R.ok("操作成功");
        }else {
            return R.ok("操作失败");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/remove/{id}")
    public R remove(@PathVariable Integer id){
        boolean b = wagesService.removeById(id);
        if (b){
            return R.ok("操作成功");
        }else{
            return R.ok("操作失败");
        }
    }

    /**
     * 查询单条数据
     * @param id
     * @param request
     * @return
     */
    @GetMapping("id/{id}")
    public R selectById(@PathVariable Integer id, HttpServletRequest request){
        Wages wages = wagesService.getById(id);
        return R.ok().put("wages",wages);
    }

    /**
     * 修改
     * @param wages
     * @return
     */
    @PostMapping("/update")
    public R updateById(Wages wages){
        boolean b =wagesService.updateById(wages);
        if (b){
            return R.ok("修改成功");
        }else {
            return R.error("修改失败");
        }
    }
}
